package com.ggj2013.hbh;

import com.eclecticdesignstudio.motion.Actuate;
import nme.display.MovieClip;
import nme.display.Bitmap;
import nme.geom.Point;
import com.ggj2013.hbh.utils.Tools;
import com.ggj2013.hbh.utils.HitBox;
import com.ggj2013.hbh.utils.OcclusionParser;
import com.ggj2013.hbh.utils.CollisionDetection;
import nme.display.MovieClip;
import nme.display.Bitmap;
import com.ggj2013.hbh.utils.Tools;

/**
 * @author Jonathan Moriarty
 */
class Room extends MovieClip {

	private var _background:Bitmap;
	private var _backgrounds:Array<Dynamic>;
	private var _bgNum:Int;
	private var _exitLocations:Array<Point>;
	private var roomName:String;
	public var walls:Array<Dynamic>;
	public var occlusions:Array<Visibility.Segment>;
	public var furnitures:Array<Furniture>;
	public var doors:Array<Door>;
	public var items:Array<Dynamic>;
	public var doorSlots:Array<Float>;
	private var occlusionPath:String;
	public var neighbors:Array<Room>;
	
	public function new( l_roomName:String, l_occlusionPath:String) {
		super();
		
		neighbors = [null,null,null,null];
		roomName = l_roomName;
		occlusionPath = l_occlusionPath;
		
		setDoorSlots();
		makeBackground();
		
		furnitures = new Array<Furniture>();
		doors = new Array<Door>();
		
		setItems();
		
		// check the bitmap, then place the walls, the door, and the occlusions based on the room.
	}		
	
	public function occlude():Void {
		var offsetX = x + Level.levelDiameter / 2;
		var offsetY = y + Level.levelDiameter / 2;
		occlusions = OcclusionParser.parse( occlusionPath, offsetX, offsetY);	
		
		
		var x;
		//left
		if (doorSlots[3] != -1 && neighbors[3] != null)
		{			
			x = 
					{ p1: 
						{ x:0+offsetX, y:0+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
					p2: 
						{ x:0+offsetX, y:doorSlots[3]+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
					d:0.0 };				
			
			occlusions.push(x);
				
			x={ p1: 
						{ x:0+offsetX, y:doorSlots[3]+Door.doorWidth+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
					p2: 
						{ x:0+offsetX, y:height+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
					d:0.0 };

			occlusions.push(x);
		} else 
		{
			occlusions.push(
					{ p1: 
						{ x:0+offsetX, y:0+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
					p2: 
						{ x:0+offsetX, y:height+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
					d:0.0 }
				);
		}
		
		//top
		if (doorSlots[0] != -1 && neighbors[0] != null)
		{
			occlusions.push(
					{ p1: 
						{ x:0+offsetX, y:0+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
					p2: 
						{ x:0+offsetX, y:doorSlots[0]+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
					d:0.0 }
				);
			
			occlusions.push(
					{ p1: 
						{ x:doorSlots[0]+Door.doorWidth+offsetX, y:0+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
					p2: 
						{ x:width+offsetX, y:0+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
					d:0.0 }
				);
		} else 
		{
			occlusions.push(
					{ p1: 
						{ x:0+offsetX, y:0+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
					p2: 
						{ x:width+offsetX, y:0+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
					d:0.0 }
				);
		}
		
		//right
		if (doorSlots[1] != -1 && neighbors[1] != null)
		{
			occlusions.push(
					{ p1: 
						{ x:width+offsetX, y:0+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
					p2: 
						{ x:width+offsetX, y:doorSlots[1]+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
					d:0.0 }
				);
				
			occlusions.push(
					{ p1: 
						{ x:width+offsetX, y:doorSlots[1]+Door.doorWidth+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
					p2: 
						{ x:width+offsetX, y:height+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
					d:0.0 }
				);
		} else 
		{
			occlusions.push(
					{ p1: 
						{ x:width-2+offsetX, y:0+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
					p2: 
						{ x:0+offsetX, y:height+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
					d:0.0 }
				);
		}
		
		//bot
		if (doorSlots[2] != -1 && neighbors[2] != null)
		{
			occlusions.push(
				{ p1: 
					{ x:0+offsetX, y:height+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
				p2: 
					{ x:doorSlots[2]+offsetX, y:height+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
				d:0.0 }
			);
			
			occlusions.push(
				{ p1: 
					{ x:doorSlots[2]+Door.doorWidth+offsetX, y:height+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
				p2: 
					{ x:width+offsetX, y:height+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
				d:0.0 }
			);
		} else 
		{
			occlusions.push(
				{ p1: 
					{ x:0+offsetX, y:height+offsetY, begin:false, segment:null, angle:0.0, visualize:false }, 
				p2: 
					{ x:width+offsetX, y:height+offsetY, begin:false, segment:null, angle:0.0, visualize:false}, 
				d:0.0 }
			);
		}
				
	}
	
	
	public function makeBackground():Void {
		//Add Background Bar
		_bgNum = 0;
		
		_backgrounds = new Array<Dynamic>();
		
		_background = Tools.loadBitmap( roomName + ".1" );
		_background.x = 0;
		_background.y = 0;
		_background.width = 3 * _background.width;
		_background.height = 3 * _background.height;
		addChild( _background );
		_backgrounds.push( _background );
		_background.alpha = 0;
		
		_background = Tools.loadBitmap( roomName + ".2" );
		_background.x = 0;
		_background.y = 0;
		_background.width = 3 * _background.width;
		_background.height = 3 * _background.height;
		addChild( _background );
		_backgrounds.push( _background );
		_background.alpha = 0;
		
		_background = Tools.loadBitmap( roomName + ".3" );
		_background.x = 0;
		_background.y = 0;
		_background.width = 3 * _background.width;
		_background.height = 3 * _background.height;
		addChild( _background );
		_backgrounds.push( _background );
		_background.alpha = 0;
		
		_background = Tools.loadBitmap( roomName + ".4" );
		_background.x = 0;
		_background.y = 0;
		_background.width = 3 * _background.width;
		_background.height = 3 * _background.height;
		addChild( _background );
		_backgrounds.push( _background );
		_background.alpha = 0;
		
		_backgrounds[ _bgNum ].alpha = 1;
		
		
		/*_background = Tools.loadBitmap( roomName );
		_background.x = 0;
		_background.y = 0;
		_background.width = 3 * _background.width;
		_background.height = 3 * _background.height;
		addChild( _background );*/
	}
	
	private function setDoorSlots():Void {
		switch ( roomName )
		{
			case "room01":
				
				doorSlots = [394, 410, 394, 410];
				
			case "room02":
				
				doorSlots = [180, 410, 180, 410]; 
				
				
			case "room03":
				
				doorSlots = [406,182,406,182]; 
				
			case "room04":
				
				doorSlots = [68,-1,68,-1]; 
				
			case "room05":
				
				doorSlots = [-1,7,-1,78]; 	
		}
	}
	
	public function setWalls():Void {
		walls = new Array<Dynamic>();
		var l_newWall:HitBox;
		Tools.trace(roomName);
		
		switch ( roomName )
		{
			case "level_bg":
				#if debug
					trace("case 1");		
				#end
				l_newWall = new HitBox( 30, 400, 0x00ff00 );
				l_newWall.x = 0;
				l_newWall.y = 0;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				l_newWall = new HitBox( 700, 30, 0x00ff00 );
				l_newWall.x = 0;
				l_newWall.y = 0;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				l_newWall = new HitBox( 30, 400, 0x00ff00 );
				l_newWall.x = 670;
				l_newWall.y = 0;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				l_newWall = new HitBox( 700, 30, 0x00ff00 );
				l_newWall.x = 0;
				l_newWall.y = 370;
				walls.push( l_newWall );
				addChild( l_newWall );
			case "room01":
				
				doorSlots = [394, 410, 394, 410];

				
				//Top left pillar
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 115;
				l_newWall.y = 120;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Mid left pillar
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 115;
				l_newWall.y = 420;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Bottom left pillar
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 115;
				l_newWall.y = 740;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Top mid large pillar
				l_newWall = new HitBox( 100, 165, 0x00ff00 );
				l_newWall.x = 385;
				l_newWall.y = 220;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Bottom mid large pillar
				l_newWall = new HitBox( 50, 135, 0x00ff00 );
				l_newWall.x = 405;
				l_newWall.y = 400;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Right top pillar
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 740;
				l_newWall.y = 120;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Right mid pillar
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 740;
				l_newWall.y = 420;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Right bottom pillar
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 740;
				l_newWall.y = 740;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Right torpedo stack
				l_newWall = new HitBox( 80, 280, 0x00ff00 );
				l_newWall.x = 815;
				l_newWall.y = 460;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Right mid large pillar
				l_newWall = new HitBox( 60, 120, 0x00ff00 );
				l_newWall.x = 830;
				l_newWall.y = 245;
				walls.push( l_newWall );
				addChild( l_newWall );
				
			case "room02":
				
				doorSlots = [180, 410, 180, 410]; 
				
				//Mid top pillar (1)
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 258;
				l_newWall.y = 10;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Mid pillar (2)
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 258;
				l_newWall.y = 160;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Mid pillar (3)
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 258;
				l_newWall.y = 341;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Mid top pillar (4)
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 258;
				l_newWall.y = 521;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Mid top pillar (5)
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 260;
				l_newWall.y = 700;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Mid bottom pillar (6)
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 258;
				l_newWall.y = 850;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Top left pillar
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 10;
				l_newWall.y = 2;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Left Torpedo Stack
				l_newWall = new HitBox( 41, 290, 0x00ff00 );
				l_newWall.x = 10;
				l_newWall.y = 42;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Mid left wall
				l_newWall = new HitBox( 1, 310, 0x00ff00 );
				l_newWall.x = 10;
				l_newWall.y = 521;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Right top Wall
				l_newWall = new HitBox( 20, 370, 0x00ff00 );
				l_newWall.x = 425;
				l_newWall.y = 130;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Right mid Wall
				l_newWall = new HitBox( 20, 120, 0x00ff00 );
				l_newWall.x = 418;
				l_newWall.y = 340;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Right top Pillar
				l_newWall = new HitBox( 60, 140, 0x00ff00 );
				l_newWall.x = 375;
				l_newWall.y = 0;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Right mid Pillar
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 405;
				l_newWall.y = 470;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Right bottom Pillar
				l_newWall = new HitBox( 10, 130, 0x00ff00 );
				l_newWall.x = 430;
				l_newWall.y = 520;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				
			case "room03":
				
				doorSlots = [406,182,406,182]; 
		
				//pillar top right
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 775;
				l_newWall.y = 40;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//pillar mid left
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 175;
				l_newWall.y = 250;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//pillar mid right
				l_newWall = new HitBox( 41, 40, 0x00ff00 );
				l_newWall.x = 330;
				l_newWall.y = 250;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//far left wall (A)
				l_newWall = new HitBox( 7, 284, 0x00ff00 );
				l_newWall.x = 103;
				l_newWall.y = 96;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//connect wall A to B
				l_newWall = new HitBox( 35, 9, 0x00ff00 );
				l_newWall.x = 110;
				l_newWall.y = 96	;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//left inner thin wall (B)
				l_newWall = new HitBox( 7, 103, 0x00ff00 );
				l_newWall.x = 145;
				l_newWall.y = 96;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//connect wall B to C (extends all the way to D)
				l_newWall = new HitBox( 300, 11, 0x00ff00 );
				l_newWall.x = 145;
				l_newWall.y = 190;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				
				//top rectangle box wall with floor circuits(C)
				l_newWall = new HitBox( 100, 160, 0x00ff00 );
				l_newWall.x = 295;
				l_newWall.y = 30;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//middle wall near floor circuit (D)
				l_newWall = new HitBox( 25, 190, 0x00ff00 );
				l_newWall.x = 425;
				l_newWall.y = 0;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//left middle wall (E)
				l_newWall = new HitBox( 20, 160, 0x00ff00 );
				l_newWall.x = 420;
				l_newWall.y = 190;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//right middle wall (F)
				l_newWall = new HitBox( 60, 145, 0x00ff00 );
				l_newWall.x = 440;
				l_newWall.y = 140;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Large torpedo Stack
				l_newWall = new HitBox( 83, 233, 0x00ff00 );
				l_newWall.x = 820;
				l_newWall.y = 215;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//Small torpedo Stack
				l_newWall = new HitBox( 83, 163, 0x00ff00 );
				l_newWall.x = 735;
				l_newWall.y = 245;
				walls.push( l_newWall );
				addChild( l_newWall );
				
			case "room04":
				
				doorSlots = [68,-1,68,-1]; 
				
				//Bottom Torpedo
				l_newWall = new HitBox( 40, 260, 0x00ff00 );
				l_newWall.x = 162;
				l_newWall.y = 180;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//large right pillar inside (right)
				l_newWall = new HitBox( 31, 190, 0x00ff00 );
				l_newWall.x = 180;
				l_newWall.y = 20;
				walls.push( l_newWall );
				addChild( l_newWall );

				//large right pillar middle
				l_newWall = new HitBox( 30, 150, 0x00ff00 );
				l_newWall.x = 155;
				l_newWall.y = 30;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//large right pillar outisde (left)
				l_newWall = new HitBox( 40, 115, 0x00ff00 );
				l_newWall.x = 125;
				l_newWall.y = 30;
				walls.push( l_newWall );
				addChild( l_newWall );
				
			case "room05":
				
				doorSlots = [-1,7,-1,78]; 

				//Right torpedo stack
				l_newWall = new HitBox( 40, 110, 0x00ff00 );
				l_newWall.x = 410;
				l_newWall.y = 0;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//right pillar
				l_newWall = new HitBox( 49, 115, 0x00ff00 );
				l_newWall.x = 333;
				l_newWall.y = 0;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//mid pillar
				l_newWall = new HitBox( 30, 130, 0x00ff00 );
				l_newWall.x = 230;
				l_newWall.y = 0;
				walls.push( l_newWall );
				addChild( l_newWall );
				
				//left-top wall
				l_newWall = new HitBox( 330, 11, 0x00ff00 );
				l_newWall.x = 0;
				l_newWall.y = 10;
				walls.push( l_newWall );
				addChild( l_newWall );		
		}
		
		//left
		if (doorSlots[3] != -1 && neighbors[3] != null)
		{
			l_newWall = new HitBox( 2, doorSlots[3], 0x00ff00 );
			l_newWall.x = 0;
			l_newWall.y = 0;
			walls.push( l_newWall );
			addChild( l_newWall );
			
			l_newWall = new HitBox( 2, height-doorSlots[3]-Door.doorWidth, 0x00ffff );
			l_newWall.x = 0;
			l_newWall.y = doorSlots[3]+Door.doorWidth;
			walls.push( l_newWall );
			addChild( l_newWall );
		} else 
		{
			l_newWall = new HitBox( 2, height, 0x00ff00 );
			l_newWall.x = 0;
			l_newWall.y = 0;
			walls.push( l_newWall );
			addChild( l_newWall );
		}
		
		//top
		if (doorSlots[0] != -1 && neighbors[0] != null)
		{
			l_newWall = new HitBox( doorSlots[0], 2, 0x00ff00 );
			l_newWall.x = 0;
			l_newWall.y = 0;
			walls.push( l_newWall );
			addChild( l_newWall );
			
			l_newWall = new HitBox( width-doorSlots[0]-Door.doorWidth, 2, 0x00ffff );
			l_newWall.x = doorSlots[0]+Door.doorWidth;
			l_newWall.y = 0;
			walls.push( l_newWall );
			addChild( l_newWall );
		} else 
		{
			l_newWall = new HitBox( width,2, 0x00ff00 );
			l_newWall.x = 0;
			l_newWall.y = 0;
			walls.push( l_newWall );
			addChild( l_newWall );
		}
		
		//right
		if (doorSlots[1] != -1 && neighbors[1] != null)
		{
			l_newWall = new HitBox(2, doorSlots[1], 0x00ff00 );
			l_newWall.x = width-2;
			l_newWall.y = 0;
			walls.push( l_newWall );
			addChild( l_newWall );
			
			l_newWall = new HitBox(2, height-doorSlots[1]-Door.doorWidth, 0x00ffff );
			l_newWall.x = width-2;
			l_newWall.y = doorSlots[1]+Door.doorWidth;
			walls.push( l_newWall );
			addChild( l_newWall );
		} else 
		{
			l_newWall = new HitBox( 2, height, 0x00ff00 );
			l_newWall.x = width-2;
			l_newWall.y = 0;
			walls.push( l_newWall );
			addChild( l_newWall );
		}
		
		//bot
		if (doorSlots[2] != -1 && neighbors[2] != null)
		{
			l_newWall = new HitBox( doorSlots[2], 2, 0x00ff00 );
			l_newWall.x = 0;
			l_newWall.y = height-2;
			walls.push( l_newWall );
			addChild( l_newWall );
			
			l_newWall = new HitBox( width-doorSlots[2]-Door.doorWidth, 2, 0x00ffff );
			l_newWall.x = doorSlots[2]+Door.doorWidth;
			l_newWall.y = height-2;
			walls.push( l_newWall );
			addChild( l_newWall );
		} else 
		{
			l_newWall = new HitBox( width, 2, 0x00ff00 );
			l_newWall.x = 0;
			l_newWall.y = height-2;
			walls.push( l_newWall );
			addChild( l_newWall );
		}
	}
	
	public function setItems():Void {
		items = new Array<Dynamic>();
		var spawnConsole = Tools.randomInt(2, 0);
		var spawnItem = -1;
		var l_item:Item;
		switch ( roomName )
		{
			case "level_bg":
				Tools.trace("case 1");		
				
			case "room01":
				var itemSelector:Array<Int> = new Array();
				itemSelector.push(Item.BELT);
				itemSelector.push(Item.EVSUIT);
				itemSelector.push(Item.MEDKIT);
				var itemIndex:Int = Tools.randomInt(3, 0);
				spawnItem = Tools.randomInt(2, 0);
				
				if (spawnItem == 1) {
					l_item = new Item( itemSelector[itemIndex], 800.0, 170.0 );
					addChild( l_item );
					items.push( l_item );
					itemSelector.splice(itemIndex, 1);
					itemIndex = Tools.randomInt(2, 0);
				}
				
				spawnItem = Tools.randomInt(2, 0);
				
				if (spawnItem == 1) {
					l_item = new Item(itemSelector[itemIndex], 320.0, 420.0 );
					addChild( l_item );
					items.push( l_item );
				}
				
				if (spawnConsole == 1) {
					l_item = new Item( Item.COMM_CONSOLE, 400.0, 830.0 );
					addChild( l_item );
					items.push( l_item );
				}
				
				
			case "room02":
				var itemSelector:Array<Int> = new Array();
				itemSelector.push(Item.CIGARETTES);
				itemSelector.push(Item.WINE);
				itemSelector.push(Item.PENNY);
				var itemIndex:Int = Tools.randomInt(3, 0);
				spawnItem = Tools.randomInt(2, 0);
				
				if (spawnItem == 1) {
					l_item = new Item(itemSelector[itemIndex], 325, 70.0 );
					addChild( l_item );
					items.push( l_item );
					itemSelector.splice(itemIndex, 1);
					itemIndex = Tools.randomInt(2, 0);
				}
				
				spawnItem = Tools.randomInt(2, 0);
				
				if (spawnItem == 1) {
					l_item = new Item(itemSelector[itemIndex], 60.0, 665.0 );
					addChild( l_item );
					items.push( l_item );
				}
				
				if (spawnConsole == 1 ) {
					l_item = new Item( Item.POWER_CONSOLE, 355.0, 680.0 );
					addChild( l_item );
					items.push( l_item );
				}
				
				
			case "room03":
				var itemSelector:Array<Int> = new Array();
				itemSelector.push(Item.CAPLOG);
				itemSelector.push(Item.SHIPLOG);
				var itemIndex:Int = Tools.randomInt(2, 0);
				spawnItem = Tools.randomInt(2, 0);
				
				l_item = new Item( Item.UNIFORM, 355.0, 205.0 );
				addChild( l_item );
				items.push( l_item );
				
				if (spawnItem == 1) {
					l_item = new Item(itemSelector[itemIndex], 170.0, 150.0 );
					addChild( l_item );
					items.push( l_item );
				}
				
				if (spawnConsole == 1) {
					l_item = new Item( Item.POD_CONSOLE, 810.0, 110.0 );
					addChild( l_item );
					items.push( l_item );
				}

				
			case "room04":
				
				var itemSelector:Array<Int> = new Array();
				itemSelector.push(Item.MIRROR);
				itemSelector.push(Item.GUN);
				var itemIndex:Int = Tools.randomInt(2, 0);
				spawnItem = Tools.randomInt(2, 0);
				
				if(spawnItem == 1) {
					l_item = new Item( itemSelector[itemIndex], 130.0, 380.0 );
					addChild( l_item );
					items.push( l_item );
				}
				
				if(spawnConsole == 1) {
					l_item = new Item( Item.BRIDGE_CONSOLE, 0.0, 200.0 );
					addChild( l_item );
					items.push( l_item );
				}
				
			case "room05":
				
				var itemSelector:Array<Int> = new Array();
				itemSelector.push(Item.WURLITZER);
				itemSelector.push(Item.MANIFEST);
				var itemIndex:Int = Tools.randomInt(2, 0);
				spawnItem = Tools.randomInt(2, 0);
				
				if(spawnItem == 1) {
					l_item = new Item( itemSelector[itemIndex], 120.0, 80.0 );
					addChild( l_item );
					items.push( l_item );
				}
		}
		//placeExit();
	}
	
	public function placeExit():Void {
	
		Tools.trace("placing exit");
		// starts 0, increments with each try, assigns a statis place after a certain number of tries.
		var exitPlaced:Int = 0;
		
		var l_exit:Item;
		if ( LevelScreen.levelNumber == LevelScreen.FINAL_LEVEL )
		{
			//trace("AIRLOCK");
			l_exit = new Item( Item.AIRLOCK_CONSOLE, Math.random() * _background.width, Math.random() * _background.height );
		}
		else
		{
			l_exit = new Item( Item.LADDER, Math.random() * _background.width, Math.random() * _background.height );
		}
		//l_exit.scaleX = l_exit.scaleY = 2.0;
		addChild( l_exit );
		items.push( l_exit );
		
		_exitLocations = new Array<Point>();
		
		switch (roomName)
		{
			case "room01":
				var exitPoint = new Point(44, 822);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(162, 47);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(314, 395);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(515, 590);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(743, 81);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(834, 393);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(822, 822);
				_exitLocations.push(exitPoint);
				
			case "room02":
				var exitPoint = new Point(18, 14);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(160, 203);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(385, 154);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(3, 481);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(17, 866);
				_exitLocations.push(exitPoint);
			
			case "room03":
				var exitPoint = new Point(15, 398);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(250, 156);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(555, 7);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(854, 7);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(765, 415);
				_exitLocations.push(exitPoint);
			
			case "room04":
				var exitPoint = new Point(44, 202);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(93, 149);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(115, 390);
				_exitLocations.push(exitPoint);
			
			case "room05":
				var exitPoint = new Point(6, 187);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(180, 30);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(287, 39);
				_exitLocations.push(exitPoint);
				
				exitPoint = new Point(410, 186);
				_exitLocations.push(exitPoint);
		}
		
		var placementChance = Tools.randomInt(_exitLocations.length, 0);
		
		l_exit.x = _exitLocations[placementChance].x;
		l_exit.y = _exitLocations[placementChance].y;
		
		/*while ( exitPlaced != -1 )
		{
			l_exit.x = Math.random() * _background.width;
			l_exit.y = Math.random() * _background.height;
			
			if ( !itemBlocked( l_exit ) )
			{
				Tools.trace("placed");
				exitPlaced = -1;
			}
			else if ( exitPlaced > 15 )
			{
				Tools.trace("give up");
				l_exit.x = 50.0;
				l_exit.y = 50.0;
				exitPlaced = -1;
			}
			else
			{
				Tools.trace("continue");
				exitPlaced++;
			}
		}*/
		//Tools.trace("EXIT");
		//Tools.trace( l_exit.x );
		//Tools.trace( l_exit.y );
	}
	
	public function itemBlocked( l_checkItem:Item ):Bool
	{
		var l_blocked:Bool = false;
		
		for ( l_wall in walls )
		{	
			//Tools.trace("wall");
			if ( CollisionDetection.isPixelPerfectColliding( l_checkItem.hitBox, l_wall, this, true, 0 ) )
			{
				l_blocked = true;
			}
		}
		
		for ( l_item in items )
		{
			//Tools.trace("item");
			if ( CollisionDetection.isPixelPerfectColliding( l_checkItem.hitBox, l_item.hitBox, this, true, 0 ) )
			{
				l_blocked = true;
			}
		}
		
		return l_blocked;
	}
	
	public function update( l_deltaTime:Int ):Void {
		var l_index:Int = items.length - 1;
		var l_item:Item;
		while ( l_index >= 0 ) {
			l_item = items[ l_index ];
			l_item.update( l_deltaTime );
			if ( l_item.remove ) {
				// set up the item event.
				l_item.visible = false;
				removeChild( l_item );
				LevelScreen.eventItem = items.splice( l_index, 1 )[ 0 ];
				LevelScreen.inItemEvent = true;
			}
			l_index--;
		}
		
		//swap out background graphic with new sanity level
		var _currV = LevelScreen.player.currentSanity;
		var _maxV = Player.MAX_SANITY;
		
		var l_ratio:Float = _currV / _maxV;
		var l_newBG:Int = -1;
		
		if ( l_ratio >= .75 )
		{
			l_newBG = 0;
		}
		else if ( l_ratio >= .5 )
		{
			l_newBG = 1;
		}
		else if ( l_ratio >= .25 )
		{
			l_newBG = 2;
		}
		else
		{
			l_newBG = 3;
		}
		
		if ( l_newBG != _bgNum )
		{
			// TODO: tween this
			Actuate.tween(_backgrounds[ l_newBG ], 4, { alpha: 1 } );
			Actuate.tween(_backgrounds[ _bgNum ], 4, { alpha: 0 } );
			//_gaugeBgs[ l_newGauge ].visible = true;
			//_gaugeBgs[ _gaugeNum ].visible = false;
			_bgNum = l_newBG;
		}
	}
}